package cn.xfdzx.android.apps.shop.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.AddAddressActivity;
import cn.xfdzx.android.apps.shop.bean.AddressListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.AddressListPresenter;
import cn.xfdzx.android.apps.shop.util.Utils;

import static cn.xfdzx.android.apps.shop.apps.Constants.START_CODE;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：管理收货地址
 */
public class ManagementAddressListAdapter extends RecyclerView.Adapter<ManagementAddressListAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<AddressListBeen.DataBean> mList;
    private Activity mContext;
    private AddressListPresenter mPresenter;
    private int defult_index;

    public ManagementAddressListAdapter(Activity context, List<AddressListBeen.DataBean> list, AddressListPresenter mPresenter) {
        this.mList = list;
        this.mContext = context;
        this.mPresenter = mPresenter;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_address_list, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        if (mList.get(position).getIs_default() == 0) {
            holder.ivSelect.setImageResource(R.drawable.select_pay_type_grey_bg);
        } else {
            defult_index = position;
            holder.ivSelect.setImageResource(R.drawable.icon_select_green);
        }
        holder.tvName.setText(mList.get(position).getRealname());
        holder.tvPhone.setText(mList.get(position).getMobile());
        if (mList.get(position).getLabel() == 1) {
            holder.tvTag.setText("家");
        } else if (mList.get(position).getLabel() == 2) {
            holder.tvTag.setText("公司");
        } else if (mList.get(position).getLabel() == 3) {
            holder.tvTag.setText("公司");
        }
        if (mList.get(position).getLabel() == 0) {
            holder.tvTag.setVisibility(View.GONE);
        } else {
            holder.tvTag.setVisibility(View.VISIBLE);
        }
        holder.tvAddress.setText(mList.get(position).getAddress_info() + mList.get(position).getHouse_number());
        holder.ivSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    if (mList.get(position).getIs_default() == 0) {
                        mPresenter.addAddres(position, defult_index, mList.get(position).getRealname(), mList.get(position).getMobile(), mList.get(position).getLng(), mList.get(position).getLat(), "1", mList.get(position).getAddress_info(), mList.get(position).getHouse_number(), mList.get(position).getId() + "", mList.get(position).getLabel() + "");
                    }
                }
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()) {
                    mItemClickListener.onItemClick(position);
                }
            }
        });
        holder.iv_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()){
                Intent intent = new Intent(mContext, AddAddressActivity.class);
                intent.putExtra("from", "address_list");
                intent.putExtra("data", mList.get(position));
                mContext.startActivityForResult(intent, START_CODE);}
            }
        });
        holder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    mPresenter.delectAddress(mList.get(position).getId() + "", position);
                }
            }
        });

    }


    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.iv_select)
        ImageView ivSelect;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_phone)
        TextView tvPhone;
        @BindView(R.id.tv_tag)
        TextView tvTag;
        @BindView(R.id.tv_address)
        TextView tvAddress;
        @BindView(R.id.tv_delete)
        TextView tvDelete;
        @BindView(R.id.iv_update)
        ImageView iv_update;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
